package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.exception.ApiException;
import com.ytjj.qmyx.admin.constants.ForumConstant;
import com.ytjj.qmyx.admin.dao.ForumLiveDao;
import com.ytjj.qmyx.admin.model.dto.ForumProductDto;
import com.ytjj.qmyx.admin.model.request.forum.ForumLiveInfoRequest;
import com.ytjj.qmyx.admin.model.request.forum.ForumLiveRequest;
import com.ytjj.qmyx.admin.model.response.ForumLiveProductResponse;
import com.ytjj.qmyx.admin.service.ForumLiveService;
import com.ytjj.qmyx.mall.model.ForumLive;
import com.ytjj.qmyx.mall.model.ProductCategory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description TODO
 * @ClassName ForumLiveServiceImpl
 *
 * 
 * @Version 1.0
 **/
@Service
public class ForumLiveServiceImpl implements ForumLiveService {

    @Resource
    private ForumLiveDao forumLiveDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addForumLive(ForumLiveRequest forumLiveRequest) {
        forumLiveDao.addForumLive(forumLiveRequest);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editForumLive(ForumLiveRequest forumLiveRequest) {
        forumLiveDao.editForumLive(forumLiveRequest);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeForumLive(Integer id) {
        forumLiveDao.removeForumLive(id);
    }

    @Override
    public CommonPage<ForumLiveProductResponse> getForumLiveProductList(Integer id, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        return CommonPage.restPage(forumLiveDao.getForumLiveProductList(id));
    }

    @Override
    public List<ForumProductDto> searchProduct(Integer productId, String productName, String platformData) {
        return forumLiveDao.searchProduct(productId, productName, platformData);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addForumProduct(Integer productId, Integer forumLiveId) {
        // 查询该版块是否添加过该商品
        Integer count = forumLiveDao.searchForumProduct(productId, forumLiveId);
        if(count != null) {
            throw new ApiException("您已经重复添加此商品,请勿重复添加！");
        }

        if(productId != null) {
            forumLiveDao.insertForumProduct(productId, forumLiveId);
        }

        /*Integer productNum = forumLiveDao.searchForumProductCount(forumLiveId);
        if(productNum <= ForumConstant.productNum) {
            if(productId != null) {
                forumLiveDao.insertForumProduct(productId, forumLiveId);
            }
            return;
        }
        throw new ApiException("商品数量已经超过6个，请删除后重试！");*/
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteForumProduct(Integer id) {
        forumLiveDao.deleteForumProduct(id);
    }

    @Override
    public void updateForumProductSort(Integer id, Integer sort) {
        forumLiveDao.updateForumProductSort(id, sort);
    }

    @Override
    public CommonPage<ForumLive> getForumLiveList(Integer status, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        return CommonPage.restPage(forumLiveDao.getForumLiveList(status));
    }

    @Override
    public void removeForum(Integer id) {
        forumLiveDao.removeForum(id);
    }

    @Override
    public List<ProductCategory> getCategory() {
        return forumLiveDao.getCategory();
    }

    @Override
    public CommonPage<ForumLiveProductResponse> searchForumLiveProductList(ForumLiveInfoRequest forumLiveInfoRequest) {
        PageHelper.startPage(forumLiveInfoRequest.getPageNum(), forumLiveInfoRequest.getPageSize());
        return CommonPage.restPage(forumLiveDao.searchForumLiveProductList(forumLiveInfoRequest));
    }

    @Override
    public void updateIsRecommend(Integer forumLiveId, Integer productId, Integer isRecommend) {
        forumLiveDao.updateIsRecommend(forumLiveId, productId, isRecommend);
    }
}
